﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS2011
{
    public partial class FolderDetailsForm : Form
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public FolderDetailsForm()
        {
            InitializeComponent();
        }

        public ServiioSharedFolder New(ServiioConnection connection)
        {
            try
            {
                Text = "Share New Folder";

                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    return new ServiioSharedFolder(
                        textBoxFolderPath.Text,
                        checkBoxMusic.Checked,
                        checkBoxPhotos.Checked,
                        checkBoxVideo.Checked,
                        checkBoxMetadata.Checked,
                        checkBoxUpdates.Checked);
                }
                return null;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return null;
            }
        }

        public bool Edit(ServiioConnection connection, ServiioSharedFolder folder)
        {
            try
            {
                Text = "Edit Shared Folder";

                buttonBrowse.Visible = false;
                textBoxFolderPath.Width = buttonBrowse.Location.X + buttonBrowse.Width - textBoxFolderPath.Left;
                textBoxFolderPath.ReadOnly = true;
                textBoxFolderPath.TabStop = false;
                textBoxFolderPath.BackColor = Color.FromArgb(231, 235, 247);

                textBoxFolderPath.Text = folder.FolderPath;
                checkBoxVideo.Checked = folder.ShareVideo;
                checkBoxMusic.Checked = folder.ShareAudio;
                checkBoxPhotos.Checked = folder.ShareImages;
                checkBoxMetadata.Checked = folder.DescriptiveMetadataSupported;
                checkBoxUpdates.Checked = folder.ScanForUpdates;

                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    folder.ShareVideo = checkBoxVideo.Checked;
                    folder.ShareAudio = checkBoxMusic.Checked;
                    folder.ShareImages = checkBoxPhotos.Checked;
                    folder.DescriptiveMetadataSupported = checkBoxMetadata.Checked;
                    folder.ScanForUpdates = checkBoxUpdates.Checked;
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return false;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                folderBrowserDialog1.Description = "Select a folder to share.";
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBoxFolderPath.Text = folderBrowserDialog1.SelectedPath;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
    }
}
